Page({
  data: {
    pic:[],
    indicatorDots: false,
    vertical: false,
    autoplay: false,
    circular: true,
    duration: 1700,
    easingfunction: "linear",
    current: 0,
    manPose:"0px 0px",
    walk:175,
    buttonClicked:false,//防止用户多次点击
    showModal: false,//弹窗展示原图
    modalSrc: "",
  },

  //从数据库获取画廊中的图片
  onShow: function(){
    let that=this;
    let picArr=[];
    wx.cloud.database().collection("galleryImage").get({
      success(res){
        console.log("res:",res)
        let dataList = res.data;
        for(let i = 0;i < dataList.length; i++){
          picArr.push(dataList[i])
        }
        that.setData({
          pic: picArr,
        })
      },
      fail(res){
        console.log("获取picArr失败", res)
      }
    })
  },

  nextImg: function() {
    this.setData({
      buttonClicked:true
    })
    var that=this;
    setTimeout(function(){
      that.setData({
        buttonClicked:false
      })
    },1700);

    var current = this.data.current;
    this.setData({
      current: current < (this.data.pic.length - 1) ? current + 1 : 0
    })
  },

  prewImg: function() {

    this.setData({
      buttonClicked:true
    })
    var that=this;
    setTimeout(function(){
      that.setData({
        buttonClicked:false
      })
    },1700);

    var current = this.data.current;
    this.setData({
      current: current > 0 ? current - 1 : this.data.pic.length - 1
    })
  },

  
  WALKnext: function(){
    var that=this;
    var startTime=new Date().getTime();
    this.setData({
      manPose:"-"+this.data.walk+"px 0px"
    })
    var int=setInterval(function(){
      var walk=that.data.walk;
      if(walk>=700){
        walk=175;
      }else{
        walk+=175;
      }
      that.setData({
        walk:walk,
        manPose:"-"+walk+"px 0px"
      })
      if(new Date().getTime() - startTime > 1700){
        clearInterval(int);
        that.setData({
          walk:175,
          manPose:"0px 0px",
        })
        return;
      }
    },170);
  },

  WALKprew: function(){
    var that=this;
    var startTime=new Date().getTime();
    this.setData({
      manPose:"-"+this.data.walk+"px -230px"
    })
    var int=setInterval(function(){
      var walk=that.data.walk;
      if(walk>=700){
        walk=175;
      }else{
        walk+=175;
      }
      that.setData({
        walk:walk,
        manPose:"-"+walk+"px -230px"
      })
      if(new Date().getTime() - startTime > 1700){
        clearInterval(int);
        that.setData({
          walk:175,
          manPose:"0px 0px",
        })
        return;
      }
    },170);
  },

  //禁止用户手动滑动翻页
  stopTouchMove: function() {
    return false;
    },

  //弹窗动态获取对应图片
  showPic: function(event){
    this.setData({
      modalSrc:  event.currentTarget.dataset.src,
      showModal: true
      })
  },

  closePic: function(){
    this.setData({
      showModal: false
      })
  },

})